<template>
  <div class="container">
    <el-button type="primary" size="small" class="add-button" @click="openAddDialog()">添加用户</el-button>
    <!-- 列表 -->
    <el-table :data="userList" border style="width: 100%">
      <el-table-column prop="userId" label="ID" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="password" label="密码" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="username" label="用户名" />

      <el-table-column prop="enable" label="性别">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex === 1">男</el-tag>
          <el-tag v-else type="info">女</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="header" label="头像" />
      <el-table-column prop="nickname" label="昵称" />
      <el-table-column prop="userEmail" label="邮箱" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row.userId)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.userId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加弹框 -->
    <el-dialog title="添加用户" :visible.sync="addDialog">
      <!-- 向另一个页面传递 方法 -->
      <userAdd @closeAddDialog="closeAddDialog" @getUserList="getUserList" />
    </el-dialog>
    <!-- 修改弹窗 -->
    <el-dialog title="修改用户" :visible.sync="updateDialog">
      <!-- 向另一个页面传递 方法 -->
      <userUpdate :user="user" @closeUpdateDialog="closeUpdateDialog" @getUserList="getUserList" />
    </el-dialog>
  </div>
</template>
<script>
// 引入接口
import userApi from '@/api/admin'
import userAdd from './user-add'
import userUpdate from './user-update'
export default {
  components: {
    userAdd,
    userUpdate
  },
  data() {
    return {
      user: {}, // 用户
      addDialog: false, // 控制添加弹窗
      updateDialog: false, // 控制修改弹窗
      userList: [] // 类型数组
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    getUserList() {
      // 查询用户列表
      userApi.findAll().then(response => {
        // console.log(response)
        this.userList = response.data
      })
    },
    handleEdit(id) {
      // 修改用户方法
      userApi.findById(id).then(response => {
        this.user = response.data
        // console.log(response.data)
        // 获取到数据
        this.updateDialog = true
      })
    },
    handleDelete(id) {
      // 删除用户方法
      this.$confirm('此操作将永久删除！, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      })
        .then(() => {
          userApi.deleteById(id).then(response => {
            this.$message.success(response.msg)
            // 删除成功 刷新列表
            this.getUserList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    openAddDialog() {
      // 打开添加用户弹窗
      this.addDialog = true
    },
    closeAddDialog() {
      // 关闭添加用户窗口
      this.addDialog = false
    },
    closeUpdateDialog() {
      // 关闭修改用户弹窗
      this.updateDialog = false
    }
  }
}
</script>

